const koa = require('koa')
const Router = require('koa-router')

const app = new koa()
const router = new Router()


// 洋葱模型
// 中间件1
app.use((ctx, next) => {
    console.log(1);
    next();
    console.log(2);
});

// 中间件 2 
app.use((ctx, next) => {
    console.log(3);
    next();
    console.log(4);
});


// 路由
router.get('/', async (ctx) => {
    ctx.body = { msg: "Hello Koa interfaces" }
})

// 配置路由
app.use(router.routes()).use(router.allowedMethods())

const port = process.env.PORT || 5000

app.listen(port, () => {
    console.log(`server started on ${port}`)
})